﻿using BuzzX8.Xmpp.StreamElements;
using BuzzX8.Xmpp.StreamElements.Factories;
using NUnit.Framework;

namespace BuzzX8.Xmpp.Test.StreamElements.Factories
{
    public abstract class DatagramElementFactoryTestBase<TFactory, TElement, TModel> : StreamElementFactoryTestBase<TFactory, TElement, TModel>
                                                                                       where TFactory : DatagramElementFactory<TElement, TModel>
                                                                                       where TElement : DatagramElement
                                                                                       where TModel : DatagramElementModel
    {
        protected void TestCreationOfErrorElement()
        {
            TModel model = CreateBaseModel();
            model.HasErrorElement = true;
            AddErrorInfo(model.ErrorElementInfo);
            DatagramElement element = CreateElement(model);

            ErrorElement errorElement = element.FindElement<ErrorElement>("error");

            VerifyErrorElement(errorElement, model.ErrorElementInfo);
        }

        protected virtual void AddErrorInfo(ErrorElementInfo errorElementInfo)
        {
            errorElementInfo.Name = "test-error-name";
            errorElementInfo.Description = "test-error-description";
            errorElementInfo.DescriptionLang = "ua";
            errorElementInfo.Details = new StreamElementImpl("error-details", "details-namespace") { Payload = "deatils description" };
        }

        protected virtual void VerifyErrorElement(ErrorElement errorElement, ErrorElementInfo errorElementInfo)
        {
            Assert.IsNotNull(errorElement);
            Assert.AreEqual(errorElementInfo.Name, errorElement.ErrorName);
            Assert.AreEqual(errorElementInfo.Description, errorElement.ErrorDescription);
            Assert.AreEqual(errorElementInfo.DescriptionLang, errorElement.DescriptionLang);
            XmlAssert.AreEquals(errorElementInfo.Details, errorElement.ErrorDetails);
        }
    }
}
